

jQuery(function ($) {
    var user_info = JSON.parse(localStorage.getItem('user_info'));
    if (!user_info) {
        window.location.href = "/login/login.html";
    }
    var id = user_info.id;
    //优惠券使用切换
    $('.myCoupon_list li').click(function () {
        var index = $(this).index();
        $(this).addClass("add_border").siblings().removeClass("add_border");
        $(".unused ul").eq(index).addClass('open').siblings().removeClass("open");
    });
    coupon_Unused(); //未使用的优惠券
    function coupon_Unused() {
        var param = {
            "[]": {
                "join": ">/Coupon/id@",
                "count": 0,
                "UserCoupon": {
                    "user_id": id,
                    "used_flag": 0
                },
                "Coupon": {
                    "id@": "[]/UserCoupon/coupon_id",
                    "end_time%": getNowFormatDate() + ",3000-01-01 00:00:00,"
                },
                "CouponScope": {
                    "coupon_id@": "[]/Coupon/id"
                }
            }
        };
        send_ajax('post', '/get', param, function (data) {
            if (data.code == 200) {
                if (data["[]"] != undefined) {
                    var data = data["[]"];
                    var el = $('.coupon_Unused');
                    coupon_list(data, el);
                }

                $('.c_item .hide').remove();
            };
        });
    }

    coupon_used(); //已使用的优惠券
    function coupon_used() {
        var param = {
            '[]': {
                "join": ">/Coupon/id@",
                'count': 0,
                'UserCoupon': {
                    'user_id': id,
                    'used_flag': 1
                },
                'Coupon': {
                    'id@': '[]/UserCoupon/coupon_id'
                },
                "CouponScope": {
                    "coupon_id@": "[]/Coupon/id"
                }
            }
        };
        send_ajax('post', '/get', param, function (data) {
            if (data.code == 200 && data["[]"]) {
                var data = data["[]"];
                var el = $('.coupon_used');
                coupon_list(data, el);
            };
        });
    }
    coupon_expired(); //已过期的优惠券
    function coupon_expired() {
        var param = {
            '[]': {
                "join": ">/Coupon/id@",
                'count': 0,
                'UserCoupon': {
                    'user_id': id,
                    'used_flag': 0
                },
                'Coupon': {
                    'id@': '[]/UserCoupon/coupon_id',
                    'end_time%': '2000-01-01 00:00:00,' + getNowFormatDate()
                },
                "CouponScope": {
                    "coupon_id@": "[]/Coupon/id"
                }
            }
        };
        send_ajax('post', '/get', param, function (data) {

            if (data.code == 200) {

                if (data["[]"] != undefined) {
                    var data = data["[]"];
                    var el = $('.coupon_expired ');
                    coupon_list(data, el);
                }
            };
        });
    }
    //优惠券列表渲染
    function coupon_list(data, el) {
        var html = "";
        for (var i = 0; i < data.length; i++) {
            if (data[i].CouponScope) {
                html += ' <li>\n                <div class="coupon_content">\n                    <div class="c_limit">' + data[i].Coupon.name + '</div>   \n                    <div class="c_time">\n                    ' + data[i].Coupon.start_time.substring(0, 16) + '-' + data[i].Coupon.end_time.substring(0, 16) + '\n                    </div>\n                </div>\n                <div class="c_msg">\n                    <div class="c_range">\n                        <div class="c_item">\n                            <span class="lable">\u4F7F\u7528\u8303\u56F4\uFF1A</span>\n                            <span class="txt ' + (data[i].CouponScope.type == 0 ? 'open' : 'hide') + '">\u5168\u54C1\u7C7B\u4F18\u60E0\u5238</span>\n                            <span class="txt ' + (data[i].CouponScope.type == 1 ? 'open' : 'hide') + '">\u6307\u5B9A\u540D\u5BB6\u53EF\u7528</span>\n                            <span class="txt ' + (data[i].CouponScope.type == 2 ? 'open' : 'hide') + '">\u6307\u5B9A\u5546\u54C1\u53EF\u7528</span>\n                            <span class="txt ' + (data[i].CouponScope.type == 3 ? 'open' : 'hide') + '">\u6307\u5B9A\u5546\u54C1\u5206\u7C7B\u53EF\u7528</span>\n                            <span class="txt ' + (data[i].CouponScope.type == 4 ? 'open' : 'hide') + '">\u6307\u5B9A\u7684\u4F1A\u5458\u7B49\u7EA7\u53EF\u7528</span>\n                        </div>\n                        <div class="c_item">\n                            <span class="lable"> \u5238\u7F16\u53F7\uFF1A</span>\n                            <span class="txt">' + data[i].Coupon.code + '</span>\n                        </div>\n                    </div>\n                    \n                </div>\n                <div class="c_btns">\n                    <a href="/xuanhua" target="_blank">\u7ACB\u5373\u4F7F\u7528</a>\n                </div>\n            </li>\n            \n            ';
            }
        }
        el.html(html);
    }
    //获取当前时间
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var strDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
        return currentdate;
    }
});